<template>
    <div class="main-content">
        <div style="width: 80%; background-color: white; margin: 30px auto; border-radius: 20px">
            <div style="padding-bottom: 10px">
                <div style="font-size: 18px; color: #000000FF; line-height: 80px; border-bottom: #cccccc 1px solid;">
                    <div style="margin-left: 20px">我的订单（{{ ordersData.length }} 个）</div>
                </div>
                <div style="margin: 20px 0; padding: 0 50px">
                    <div class="table">
                        <el-table :data="ordersData" strip>
                            <el-table-column label="商品图片" width="100px">
                                <template v-slot="scope">
                                    <el-image style="width: 80px; height: 60px; border-radius: 3px" v-if="scope.row.goodsImg"
                                              :src="scope.row.goodsImg" :preview-src-list="[scope.row.goodsImg]"></el-image>
                                </template>
                            </el-table-column>
                            <el-table-column prop="orderNo" label="订单编号"></el-table-column>
                            <el-table-column prop="goodsName" label="商品名称" :show-overflow-tooltip="true">
                                <template v-slot="scope">
                                    <a :href="'/front/detail?id=' + scope.row.goodsId">{{scope.row.goodsName}}</a>
                                </template>
                            </el-table-column>
                            <el-table-column prop="businessName" label="店铺名称">
                                <template v-slot="scope">
                                    <a :href="'/front/business?id=' + scope.row.businessId">{{scope.row.businessName}}</a>
                                </template>
                            </el-table-column>
                            <el-table-column prop="num" label="商品数量" width="50"></el-table-column>
                            <el-table-column prop="price" label="订单总价" width="60"></el-table-column>
                            <el-table-column prop="username" label="收货人"></el-table-column>
                            <el-table-column prop="useraddress" label="收货地址"></el-table-column>
                            <el-table-column prop="phone" label="联系电话"></el-table-column>
                            <el-table-column prop="status" label="订单状态"></el-table-column>
                            <el-table-column prop="payNo" label="交易号"></el-table-column>
                            <el-table-column prop="payTime" label="支付时间"></el-table-column>
                            <el-table-column label="操作" align="center" width="180">
                                <template v-slot="scope">
                                    <el-button size="mini" type="primary" v-if="scope.row.status === '待支付'" plain @click="pay(scope.row)">支付</el-button>
                                    <el-button size="mini" type="primary" v-if="scope.row.status === '待收货'" plain @click="updateStatus(scope.row, '已完成')">确认收货</el-button>
                                    <el-button size="mini" type="primary" v-if="scope.row.status === '已完成'" plain @click="addComment(scope.row)">评价</el-button>
                                    <el-button size="mini" type="danger" plain @click="del(scope.row.id)">删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>

                        <el-dialog title="请输入评价内容" :visible.sync="fromVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
                            <el-form :model="form" label-width="100px" style="padding-right: 50px" ref="formRef">
                                <el-form-item label="评价内容" prop="username">
                                    <el-input type="textarea" v-model="form.content" placeholder="请输入评价内容"></el-input>
                                </el-form-item>
                            </el-form>

                            <div slot="footer" class="dialog-footer">
                                <el-button @click="fromVisible = false">取 消</el-button>
                                <el-button type="primary" @click="save">确 定</el-button>
                            </div>
                        </el-dialog>

                        <div class="pagination" style="margin-top: 20px">
                            <el-pagination
                                    background
                                    @current-change="handleCurrentChange"
                                    :current-page="pageNum"
                                    :page-sizes="[5, 10, 20]"
                                    :page-size="pageSize"
                                    layout="total, prev, pager, next"
                                    :total="total">
                            </el-pagination>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>

export default {

    data() {
        return {
            user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
            ordersData: [],
            pageNum: 1,   // 当前的页码
            pageSize: 10,  // 每页显示的个数
            total: 0,
            form: {},
            fromVisible: false
        }
    },
    mounted() {
        this.loadOrders(1)
    },
    // methods：本页面所有的点击事件或者其他函数定义区
    methods: {
        loadOrders(pageNum) {
            if (pageNum) this.pageNum = pageNum
            this.$request.get('/orders/selectPage', {
                params: {
                    pageNum: this.pageNum,
                    pageSize: this.pageSize,
                }
            }).then(res => {
                if (res.code === '200') {
                    this.ordersData = res.data?.list
                    this.total = res.data?.total
                } else {
                    this.$message.error(res.msg)
                }
            })
        },
        navTo(url) {
            location.href = url
        },
        del(id) {
            this.$request.delete('/orders/delete/' + id).then(res => {
                if (res.code === '200') {
                    this.$message.success('删除成功')
                    this.loadOrders(1)
                } else {
                    this.$message.error(res.msg)
                }
            })
        },
        handleCurrentChange(pageNum) {
            this.loadOrders(pageNum)
        },
        updateStatus(row, status) {
            this.form = row
            this.form.status = status
            this.$request.put('/orders/update', this.form).then(res => {
                if (res.code === '200') {
                    this.$message.success('操作成功')
                } else {
                    this.$message.error(res.msg)
                }
            })
        },
        addComment(row) {
            this.fromVisible = true
            this.form = row
        },
        save() {
            let data = {
                userId: this.user.id,
                businessId: this.form.businessId,
                goodsId: this.form.goodsId,
                content: this.form.content,
            }
            this.$request.post('/comment/add', data).then(res => {
                if (res.code === '200') {
                    this.$message.success('评价成功')
                    this.fromVisible = false
                    this.updateStatus(this.form, '已评价')
                    this.form = {}
                } else {
                    this.$message.error(res.msg)
                }
            })
        },
        pay(row) {
            window.open('http://localhost:9090/alipay/pay?orderNo=' + row.orderNo)
        }
    }
}
</script>
